<template>
  <div class="app-container">
    <el-form
      ref="userForm"
      :model="user"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input
          v-model="user.oldPwd"
          show-password
          placeholder="请输入旧密码"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input
          v-model="user.newPwd"
          show-password
          placeholder="请输入新密码"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model="user.confirmPassword"
          show-password
          placeholder="请再次输入新密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.go(-1)">返回</el-button>
        <el-button @click="resetForm('userForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('userForm')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import userApi from '@/api/sys/user'

export default {
  data() {
    return {
      user: {
        oldPwd: '',
        newPwd: '',
        confirmPassword: ''
      },
      rules: {
        oldPwd: [
          { required: true, message: '旧密码不能为空', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码不能少于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 确认密码
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.user.newPwd) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },

    // 提交
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          userApi.updatePwd(this.user).then(() => {
            this.$message.success('密码修改成功')
            setTimeout(() => {
              this.$confirm('您的密码已经修改请重新登录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                beforeClose: (action, instance, done) => {
                  if (action === 'confirm') {
                    instance.confirmButtonLoading = true
                    instance.confirmButtonText = '正在退出登录中...'
                    setTimeout(() => {
                      done()
                      setTimeout(() => {
                        instance.confirmButtonLoading = false
                      }, 200)
                    }, 2000)
                  } else {
                    done()
                  }
                }
              })
                .then(async () => {
                  await this.$store.dispatch('user/logout')
                  this.$router.push(`/login?redirect=${this.$route.fullPath}`)
                  this.$message.success('成功退出登录')
                })
                .catch(async () => {
                  await this.$store.dispatch('user/logout')
                  this.$router.push(`/login?redirect=${this.$route.fullPath}`)
                  this.$message.success('成功退出登录')
                })
            }, 2000)
          })
        }
      })
    },

    // 重置
    resetForm(form) {
      this.$refs[form].resetFields()
      this.getUserInfo()
    }
  }
}
</script>
